from selenium import webdriver
from selenium.webdriver.chrome.service import Service  # 使用路径的对象
from selenium.webdriver.common.by import By  # 导入定位的方法
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait

# 新版本的写法 3.10
# service = Service('f:\\chrom\\chromedriver.exe')
# driver = webdriver.Chrome(service=service)
# 老版本才这样写 3.8
# exe_path = 'f:\\chrom\\chromedriver.exe'
# driver = webdriver.Chrome(executable_path=exe_path)


# 获取百度页面

service = Service('f:\\chrom\\chromedriver.exe')
driver = webdriver.Chrome(service=service)

driver.get('https://www.baidu.com/?tn=88093251_33_hao_pg')
# input_obj = driver.find_element(By.ID, 'kw')  # 通过id 获取输入框
input_obj = driver.find_element(By.XPATH, '//*[@id="kw"]')
input_obj.send_keys('蟒蛇')  # send_keys 输入数据

btu_obj = driver.find_element(By.ID, 'su')
btu_obj.click()

# 页面没有加载出来就进行了打印 页面等待操作
wait = WebDriverWait(driver, 10)  # 等待10秒，有数据就进行操作，没有就报错
element_obj = EC.presence_of_element_located((By.XPATH, '//*[@id="2"]/div/h3/a'))
wait.until(element_obj).click()

# btu_obj = driver.find_element(By.XPATH, '//*[@id="2"]/div/h3/a')
# btu_obj.click()


print(driver.page_source)  # 打印检查里面的代码
# wait.until()
input()  # 防止页面关闭

# 使用selenium模拟登录 17小说网 https://passport.17k.com/
